<?php
class ModelNews
{
	public function construct()
	{
		
	}
	
	public function listNews()
	{
		DB::exec("SET NAMES 'utf8'");
		
		$sql = "SELECT news_id, news_title, news_desc_en, news_desc_sin, news_desc_tamil, status
				FROM tbl_news";
		$news_arr = DB::fetchAll($sql);
		
		return $news_arr;
	}
	
	public function saveNews($formVal)
	{
		$news_title = $formVal['news_title'];
		$newscontent_en = $formVal['newscontent_en'];
		
		if($formVal['newscontent_sin'] == "")
		{
			$newscontent_sin = $formVal['newscontent_en'];
		} else {
			$newscontent_sin = $formVal['newscontent_sin'];
		}
		
		if($formVal['newscontent_tamil'] == "")
		{
			$newscontent_tamil = $formVal['newscontent_en'];
		} else {
			$newscontent_tamil = $formVal['newscontent_tamil'];
		}
		
		$status = $formVal['status'];
		
		DB::exec("SET NAMES 'utf8'");
		$sql = "INSERT INTO tbl_news(news_title, news_desc_en, news_desc_sin, news_desc_tamil, status)
				VALUES('" . $news_title . "', '" . addslashes($newscontent_en) . "', '" . addslashes($newscontent_sin) . "', '" . addslashes($newscontent_tamil) . "', " . $status . ")";
		$save_news = DB::exec($sql);
		
		if($save_news)
		{
			echo "<span class='success'>Record has been successfully added</span>";
		}
	}
	
	public function getNewsById($news_id)
	{
		DB::exec("SET NAMES 'utf8'");
		$sql = "SELECT news_title, news_desc_en, news_desc_sin, news_desc_tamil, status
				FROM tbl_news 
				WHERE news_id = " . $news_id;
		
		$news_arr = DB::fetchAll($sql);
		
		return $news_arr;
	}
	
	public function updateNews($formVal)
	{
		$news_id = $formVal['news_id'];
		$news_title = $formVal['news_title'];
		$news_desc_en = $formVal['news_desc_en'];
		$news_desc_sin = $formVal['news_desc_sin'];
		$news_desc_tamil = $formVal['news_desc_tamil'];
		$status = $formVal['status'];
		
		DB::exec("SET NAMES 'utf8'");
		$sql = "UPDATE tbl_news
				SET news_title = '" . $news_title . "', news_desc_en = '" . addslashes($news_desc_en) . "', news_desc_sin = '" . addslashes($news_desc_sin) . "', news_desc_tamil = '" . addslashes($news_desc_tamil) . "', status = " . $status . "
				WHERE news_id = " . $news_id;
		
		$update_news = DB::exec($sql);
		
		if($update_news)
		{
			echo "<span class='success'>Record has been successfully updated</span>";
		}		
	}
	
	public function delNews($formVal)
	{
		$news_id = $formVal['news_id'];
		
		$sql = "DELETE FROM tbl_news
				WHERE news_id = " . $news_id;
		
		$del_news = DB::exec($sql);
		
		if($del_news)
		{
			echo "<span class='success'>Record has been successfully deleted</span>";
		}
	}
}
?>